Skip to content

docs: fix example for channel, do not assume a single-threaded dispatcher #4417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

murfel
Copy link
Contributor

@murfel murfel commented Apr 21, 2025

This example illustrating when send suspends works on a single-threaded dispatcher, but is send guaranteed to suspend on a multithreaded dispatcher?

val channel = Channel<Int>()
launch {
    check(channel.receive() == 5)
}
channel.send(5) // suspends until 5 is received

Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example could indeed benefit from more comments. To answer your question: it is guaranteed in all scenarios, because the default argument for the channel's buffer size is RENDEZVOUS, which means no buffer gets created. Could you add something like // a channel without a buffer to the code?

I don't understand how delay(100) helps, though.

@murfel
Copy link
Contributor Author

murfel commented Jul 7, 2025

Discussed that suspends in the docs should be interpreted as may or may not suspend, but is a suspension point, and the user shouldn't think or base their code on whether the function will actually suspend, hence it is counterproductive to sprinkle the doc snippets with yield().

@murfel murfel closed this Jul 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants